package com.xc.freeapp.account.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.entity.Suggest;
import com.xc.freeapp.entity.UserInfo;
import com.xc.freeapp.entity.Users;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.SuggestService;
import com.xc.freeapp.service.UserService;
import com.xc.freeapp.service.UsersService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.DateUtils;
import com.xc.freeapp.util.TokenAnnotation;

@Controller
@RequestMapping("/suggest")
@Api(value = "/suggest", description = "意见反馈")
public class SuggestController extends AbstractController {
	@Autowired
	private SuggestService suggestServiceImpl;
	@Autowired
	UsersService usersService;

	/**
	 * 意见反馈接口
	 * 
	 * @param request
	 * @return
	 */
	@TokenAnnotation(required = false)
	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加意见反馈")
	public ResponseBean suggestBack(@RequestBody @Valid Suggest suggest)
			throws BaseException {
		try {
			AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
			suggest.setBusinessid(authInfo.getAppIntId());
			suggest.setUserid(authInfo.getUserIntId());
			Users users = usersService.getUserInfo(authInfo.getUserId());
			suggest.setTelephone(users.getTelephone());
			suggest.setCreatetime(DateUtils.getDate());
			suggestServiceImpl.saveSuggest(suggest);
			return ResponseBean.getSuccess();
		} catch (BaseException e) {
			throw e;
		} catch (Exception e) {
			throw new BaseException(Constant.COMMON_ERROR);
		}
	}
}
